package com.sinsz.wxpn;

import com.sinsz.wxpn.open.base.BaseService;
import com.sinsz.wxpn.open.menu.MenuService;
import com.sinsz.wxpn.open.message.*;
import org.springframework.context.ApplicationContext;

/**
 * 微信公众号接口调用建造者对象
 *
 * @author chenjianbo
 * @date 2018-11-12
 */
public class WxpnBean {

    private final BaseService base;

    private final AcceptService accept;

    private final ReplyService reply;

    private final Handler handler;

    private final ApplicationContext context;

    private final TemplateMessageService template;

    private final MenuService menu;

    public BaseService getBase() {
        return base;
    }

    public AcceptService getAccept() {
        return accept;
    }

    public ReplyService getReply() {
        return reply;
    }

    public Handler getHandler() {
        return handler;
    }

    ApplicationContext getContext() {
        return context;
    }

    public TemplateMessageService getTemplate() {
        return template;
    }

    public MenuService getMenu() {
        return menu;
    }

    private WxpnBean(Builder builder) {
        this.base = builder.base;
        this.accept = builder.accept;
        this.reply = builder.reply;
        this.handler = builder.handler;
        this.context = builder.context;
        this.template = builder.template;
        this.menu = builder.menu;
    }

    static class Builder {

        WxpnBean build() {
            return new WxpnBean(this);
        }

        /**
         * 基本接口
         */
        private BaseService base;

        Builder setBase(BaseService base) {
            this.base = base;
            return this;
        }

        /**
         * 接收消息
         */
        private AcceptService accept;

        Builder setAccept(AcceptService accept) {
            this.accept = accept;
            return this;
        }

        /**
         * 回复消息
         */
        private ReplyService reply;

        Builder setReply(ReplyService reply) {
            this.reply = reply;
            return this;
        }

        /**
         * 消息处理器
         */
        private Handler handler;

        <T> Builder setHandler(Class<T> tClass) {
            try {
                if (tClass == null) {
                    return this;
                }
                T handler = tClass.newInstance();
                if (!(handler instanceof Handler)) {
                    return this;
                }
                this.handler = (Handler) tClass.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace(System.out);
            }
            return this;
        }

        /**
         * 上下文
         */
        private ApplicationContext context;

        Builder setContext(ApplicationContext context) {
            this.context = context;
            return this;
        }

        /**
         * 模板消息
         */
        private TemplateMessageService template;

        Builder setTemplate(TemplateMessageService template) {
            this.template = template;
            return this;
        }

        /**
         * 自定义菜单
         */
        private MenuService menu;

        Builder setMenu(MenuService menu) {
            this.menu = menu;
            return this;
        }

    }

}
